//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for TagBreadCrumbs.
	/// </summary>
	public class TagBreadCrumb : Literal 
	{
		protected CSContext csContext = CSContext.Current;
		protected Queue crumbs = new Queue();
		protected HtmlAnchor anchor;

		#region Properties
	
		/// <summary>
		/// Controls whether or not the root element for the home is shown
		/// </summary>
		[
		System.ComponentModel.DefaultValue( true ),
		]
		public virtual Boolean ShowHome 
		{
			get 
			{
				Object state = ViewState["ShowHome"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return true;
			}
			set 
			{
				ViewState["ShowHome"] = value;
			}
		}

		[
		System.ComponentModel.DefaultValue( true ),
		]
		public virtual Boolean ShowAllTags 
		{
			get 
			{
				Object state = ViewState["ShowAllTags"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return true;
			}
			set 
			{
				ViewState["ShowAllTags"] = value;
			}
		}

		/// <summary>
		///  Determines whether or not links are hook-ed up.
		/// </summary>
		[
		System.ComponentModel.DefaultValue( true ),
		]
		public virtual Boolean EnableLinks 
		{
			get 
			{
				Object state = ViewState["EnableLinks"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return true;
			}
			set 
			{
				ViewState["EnableLinks"] = value;
			}
		}

		#endregion


		protected override void CreateChildControls() 
		{
			// Do we need to add the home link?
			if (ShowHome) 
				crumbs.Enqueue(GetAnchor(csContext.SiteSettings.SiteName, SiteUrls.Instance().Home));

			if (ShowAllTags || (csContext.Tags != null && csContext.Tags.Length > 0))
				crumbs.Enqueue(GetAnchor(ResourceManager.GetString("TagBreadCrumb_Root"), GetTagUrl()));

			AddTags();
		}

		protected virtual void AddTags()
		{
			if (csContext.Tags != null)
			{
				ArrayList tags = new ArrayList();
				foreach (string tag in csContext.Tags)
				{
					tags.Add(tag);
					crumbs.Enqueue(GetAnchor(tag, GetTagUrl((string[]) tags.ToArray(typeof(string)))));
				}
			}
		}

		protected virtual string GetTagUrl()
		{
			return SiteUrls.Instance().TagsHome;
		}

		protected virtual string GetTagUrl(string[] tagsPath)
		{
			return SiteUrls.Instance().TagsBrowser(tagsPath);
		}

		protected virtual string GetTagRssUrl(string[] tagsPath)
		{
			if(csContext.SiteSettings.EnableRssSearch && csContext.Tags != null && csContext.Tags.Length > 0)
			{
				SearchQuery query = new SearchQuery();
				query.Tags = csContext.Tags;
				query.LogicallyOrTags = false;
				query.SortBy = SearchSort.DateDescending;

				return Globals.GetSiteUrls().SearchRss(SearchProvider.Instance().SearchQueryString(query));
			}
			else
				return null;
		}

		protected Control GetAnchor(string innerText, string href) 
		{
			anchor = new HtmlAnchor();
			anchor.InnerHtml = innerText;
			anchor.Attributes["class"] = "lnk3";
			anchor.HRef = href;

			return anchor;
		}

		protected override void Render(HtmlTextWriter writer) 
		{
			while(crumbs.Count > 0) 
			{
				HtmlAnchor a = (HtmlAnchor) crumbs.Dequeue();

				// Add title for hover over, 
				// but html decoded
				//
				a.Title = Globals.HtmlDecode( a.InnerHtml );

				if ((crumbs.Count > 0) && (a.InnerHtml.Length > 15))
					a.InnerHtml = a.InnerHtml.Substring(0,15) + "...";

				a.RenderControl(writer);

				if (crumbs.Count > 0)
					writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
				else
				{
					// Create RSS link next to the last tag
					string rssUrl = GetTagRssUrl(csContext.Tags);
					if (!Globals.IsNullorEmpty(rssUrl))
					{
						HtmlAnchor rss = new HtmlAnchor();
						rss.HRef = rssUrl;
						rss.InnerHtml = ResourceManager.GetString("TagBreadCrumb_Rss");
						rss.Title = rss.InnerHtml;
						rss.Attributes["class"] = "lnk3";
						writer.Write(" &nbsp; ");
						rss.RenderControl(writer);
					}
				}

			}
		}
	}
}
